﻿using Bricks.Business.Models.Account;
using Bricks.Component.Utils;
using Bricks.Business.Data.Repositories.Account;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;

namespace Bricks.Business.Service.Impl
{
    /// <summary>
    /// 功能管理
    /// </summary>
    [Export(typeof(IActionInfoService))]
    public class ActionInfoService : IActionInfoService
    {
        [Import]
        protected IActionInfoRepository ActionRepository { get; set; }

        public IQueryable<ActionInfo> Actions
        {
            get { return ActionRepository.Entities; }
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public OperationResult Add(ActionInfo model)
        {
            ActionInfo record = model.CopyTo<ActionInfo>();
            record.ID = GuidHelper.NewGuid();
            record.OrderNo = ActionRepository.GetNextOrderNo(m => m.OrderNo);  //获取下一个排序号
            ActionRepository.Insert(record);
            return new OperationResult(OperationResultType.Success, "新增成功", record);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public OperationResult Modify(ActionInfo model)
        {
            PublicHelper.CheckArgument(model, "model");
            ActionInfo record = Actions.FirstOrDefault(m => m.ID == model.ID);
            if (record == null)
                return new OperationResult(OperationResultType.QueryNull, "未找到指定的记录");

            record = model.CopyTo<ActionInfo>();
            if (ActionRepository.Update(record) > 0)
                return new OperationResult(OperationResultType.Success, "修改成功", record);
            return new OperationResult(OperationResultType.Error, "保存失败");
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public OperationResult Delete(Guid id)
        {
            ActionInfo record = Actions.FirstOrDefault(m => m.ID == id);
            if (record == null)
                return new OperationResult(OperationResultType.QueryNull, "未找到指定的记录");

            if (ActionRepository.Delete(record) >= 0)
                return new OperationResult(OperationResultType.Success, "删除成功", record);
            return new OperationResult(OperationResultType.Error, "删除失败");
        }

        /// <summary>
        /// 排序设置(向上向下)
        /// </summary>
        /// <param name="id"></param>
        /// <param name="pid"></param>
        /// <param name="upOrDown"></param>
        /// <returns></returns>
        public OperationResult SetOrder(Guid id, string upOrDown)
        {
            IQueryable<ActionInfo> entities = ActionRepository.Entities;  //找出可能涉及排序的实体集合（同一PID下）
            var exeResult = ActionRepository.SetOrder(entities, id, "OrderNo", upOrDown);  //执行向上向下操作
            return exeResult;
        }
    }
}
